<html>

<head>
	<style type="text/css">
		body {
			font-family: Calibri, Tahoma, sans-serif;
		}
		table {
			border-collapse:collapse;
			border: 1px solid black;
		}
		tr.alt {
			background-color: #E8EAFF;
		}
		td {
			padding: 5px 25px;
			text-align: center;
			border: 0px;
			color: #495674;
			white-space: nowrap;	/* No line break at hyphens */
		}
		th {
			font-size: 1.2em;
			border: 0px;
			border-bottom: 1px dashed #354255;
			padding: 10px 15px;
			background-color: #AEC8F5;
			color: #354255;
		}
	</style>
</head>

<body>

<center>
<h1>Movie Database Query</h1>
<form action="query.php" method="GET">
	<textarea name="query" cols="60" rows="8"><?php $query = isset($_GET['query']) ? $_GET['query'] : "Enter your query here."; echo $query; ?></textarea>
	</br>
	<input type="submit" value="Submit Query"/>
</form>
	
<?php
if($_GET["query"]) {
	$query = $_GET["query"];
	$imdb_connection = mysql_connect("localhost", "cs143", "");
	mysql_select_db("CS143", $imdb_connection);
	$rs = mysql_query($query, $imdb_connection);
	$num_query_fields = mysql_num_fields($rs);
	
	function create_table($attr) {
		global $num_query_fields;
		global $row_num;
		if ($row_num%2)
			echo "<tr>";
		else
			echo '<tr class="alt">';
		for($i = 0; $i < $num_query_fields; ++$i) {
			if(is_null($attr[$i]))
				echo "<td> - </td>";
			else
				echo "<td>" . $attr[$i] . "</td>";
		}
		echo "</tr>";
	}
	echo "<h2>The result of your query: </h2>";
	echo "<table class='data_table' border='1'>";
	echo "<tr align=center>";
	for($j = 0; $j < $num_query_fields; ++$j) {
		echo "<th>" . strtoupper(mysql_field_name($rs, $j)) . "</th>";
	}
	echo "</tr>";
	$row_num = 0;
	while($row = mysql_fetch_row($rs)) {
		global $row_num;
		++$row_num;
		create_table($row);
	}
	echo "</table>";
	mysql_close($imdb_connection);
}
?>
</center>

</body>
</html>
